%%
%% %CopyrightBegin%
%%
%% Copyright Hillside Technology Ltd. 2016. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%     http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% %CopyrightEnd%
%%

%% Modified to test soap with attchements: getFulltextXML echos the received attachment if
%% there is any, otherwise sends 2 short attachments.

%% generated by soap from: e:/e_soap/soap/examples and testcases/europepmc/europepmc.wsdl
%% for service "WSCitationImplService" and port "WSCitationImplPort"
%% using options: [{service,"WSCitationImplService"},{port,"WSCitationImplPort"},{generate,both},{namespaces,[{"http://ws-i.org/profiles/basic/1.1/xsd","P0"},{"http://webservice.cdb.ebi.ac.uk/",undefined}]},{http_server,soap_server_cowboy},{server_name,"europepmc_server"},{http_client,soap_client_ibrowse},{client_name,"europepmc_client"},{attachments,true}]


-module(europepmc_server).

-include("europepmc.hrl").

-export([searchPublications/3]).
-export([profilePublications/3]).
-export([getCitations/3]).
-export([getReferences/3]).
-export([getTextMinedTerms/3]).
-export([getDatabaseLinks/3]).
-export([getSupplementaryFiles/3]).
-export([getFulltextXML/3]).
-export([getBookXML/3]).
-export([listSearchFields/3]).
-export([getLabsLinks/3]).
-export([interface/0]).
-export([exception/7]).


%% replace the default exception handler to include a stacktrace.
-spec exception(Class::atom(), Reason::any(), Stacktrace::any(),
                soap:soap_fault_code(), Description::string(), soap:soap_req(), 
                soap:soap_handler_state()) -> soap:soap_handler_response().
exception(Class, Reason, Stacktrace, Type, _Desc, Soap_req, Handler_state) ->
    Message = io_lib:format("exception, class: ~p, reason: ~p,~nstack: ~P~n", 
                            [Class, Reason, Stacktrace, 14]),
    {fault, soap_fault:fault(Type, Message, Soap_req), Soap_req, Handler_state}. 

-spec searchPublications(Parsed_body::searchPublications(),
    Soap_req::soap:soap_req(), State::soap:soap_handler_state())
    -> soap:soap_handler_response(searchPublicationsResponse()).
searchPublications(_Parsed_body, Soap_req, State) ->
    %% your implementation goes here
    {ok, #searchPublicationsResponse{}, Soap_req, State}.

-spec profilePublications(Parsed_body::profilePublications(),
    Soap_req::soap:soap_req(), State::soap:soap_handler_state())
    -> soap:soap_handler_response(profilePublicationsResponse()).
profilePublications(_Parsed_body, Soap_req, State) ->
    %% your implementation goes here
    {ok, #profilePublicationsResponse{}, Soap_req, State}.

-spec getCitations(Parsed_body::getCitations(),
    Soap_req::soap:soap_req(), State::soap:soap_handler_state())
    -> soap:soap_handler_response(getCitationsResponse()).
getCitations(_Parsed_body, Soap_req, State) ->
    %% your implementation goes here
    {ok, #getCitationsResponse{}, Soap_req, State}.

-spec getReferences(Parsed_body::getReferences(),
    Soap_req::soap:soap_req(), State::soap:soap_handler_state())
    -> soap:soap_handler_response(getReferencesResponse()).
getReferences(_Parsed_body, Soap_req, State) ->
    %% your implementation goes here
    {ok, #getReferencesResponse{}, Soap_req, State}.

-spec getTextMinedTerms(Parsed_body::getTextMinedTerms(),
    Soap_req::soap:soap_req(), State::soap:soap_handler_state())
    -> soap:soap_handler_response(getTextMinedTermsResponse()).
getTextMinedTerms(_Parsed_body, Soap_req, State) ->
    %% your implementation goes here
    {ok, #getTextMinedTermsResponse{}, Soap_req, State}.

-spec getDatabaseLinks(Parsed_body::getDatabaseLinks(),
    Soap_req::soap:soap_req(), State::soap:soap_handler_state())
    -> soap:soap_handler_response(getDatabaseLinksResponse()).
getDatabaseLinks(_Parsed_body, Soap_req, State) ->
    %% your implementation goes here
    {ok, #getDatabaseLinksResponse{}, Soap_req, State}.

-spec getSupplementaryFiles(Parsed_body::getSupplementaryFiles(),
    Soap_req::soap:soap_req(), State::soap:soap_handler_state())
    -> soap:soap_handler_response(getSupplementaryFilesResponse()).
getSupplementaryFiles(_Parsed_body, Soap_req, State) ->
    %% your implementation goes here
    {ok, #getSupplementaryFilesResponse{}, Soap_req, State}.

-spec getFulltextXML(Parsed_body::getFulltextXML(),
    Soap_req::soap:soap_req(), State::soap:soap_handler_state())
    -> soap:soap_handler_response(getFulltextXMLResponse()).
getFulltextXML(#getFulltextXML{id = Id, source = Source}, Soap_req, State) ->
    Resp_attachments = 
        case soap_req:req_attachments(Soap_req) of
            [] ->
                [{[{<<"Content-ID">>, <<"1">>}], <<"attachment 1">>},
                 {[{<<"Content-ID">>, <<"2">>}], <<"\nattachment 2\n">>}];
            Attachments ->
                Attachments
        end, 
    {ok, #getFulltextXMLResponse{
            return = #result{request = #request{id = Id, source = Source},
                             pmcid = Id,
                             source = Source}}, 
     soap_req:set_resp_attachments(Soap_req, Resp_attachments), 
     State}.

-spec getBookXML(Parsed_body::getBookXML(),
    Soap_req::soap:soap_req(), State::soap:soap_handler_state())
    -> soap:soap_handler_response(getBookXMLResponse()).
getBookXML(_Parsed_body, Soap_req, State) ->
    %% your implementation goes here
    {ok, #getBookXMLResponse{}, Soap_req, State}.

-spec listSearchFields(Parsed_body::listSearchFields(),
    Soap_req::soap:soap_req(), State::soap:soap_handler_state())
    -> soap:soap_handler_response(listSearchFieldsResponse()).
listSearchFields(_Parsed_body, Soap_req, State) ->
    %% your implementation goes here
    {ok, #listSearchFieldsResponse{}, Soap_req, State}.

-spec getLabsLinks(Parsed_body::getLabsLinks(),
    Soap_req::soap:soap_req(), State::soap:soap_handler_state())
    -> soap:soap_handler_response(getLabsLinksResponse()).
getLabsLinks(_Parsed_body, Soap_req, State) ->
    %% your implementation goes here
    {ok, #getLabsLinksResponse{}, Soap_req, State}.

%% The 'interface()' function is used by the SOAP framework to access information about
%% the WSDL.
interface() ->
    ?INTERFACE.
